package ee.subjects.frontend.controller.command.personCommands;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import ee.subjects.backend.model.data.Address;

import ee.subjects.backend.model.data.Contact;
import ee.subjects.backend.model.data.Person;
import ee.subjects.backend.model.data.Subject_attribute;
import ee.subjects.backend.model.data.subjectdbtypetablesconstants.SubjectDBTypeTablesConstants;
import ee.subjects.backend.services.PersonService;
import ee.subjects.backend.services.PersonServiceImpl;
import ee.subjects.frontend.controller.command.Command;
import ee.subjects.frontend.controller.model.CustomAttripute;

public class EditPersonCommand implements Command {

	@Override
	public int execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		try{
			if (request.getParameter("id") != null){
				int personId=Integer.parseInt(request.getParameter("id"));
				
				PersonService PersonService = new PersonServiceImpl();
				
				Person person = PersonService.getPersonById(personId);
				request.setAttribute ("Person", person);
				
				// Start Formatting addresses
				List<Address> allAddresses = PersonService.getAllPersonAddressesWithAddress_type(personId); 
				
				// find sort main and additional addresses
				Address mainAddress=null;
				List<Address> additionalAddresses = new ArrayList<Address>();
				int personMainAddressType=SubjectDBTypeTablesConstants.ADDRESS_TYPE_PERSON_MAIN_ADDRESS;
				for (Address address : allAddresses){
					if(address.getAddress_type().getAddress_type()==personMainAddressType){
						mainAddress=address;
					}else{
						additionalAddresses.add(address);
					}
				}
				// End Formatting addresses
				
				// Formatting attriputes
				List<Subject_attribute> personSpecificAttributes = PersonService.getPersonSpecificAttributes(personId);
				List<CustomAttripute> personSpecificAttributesForView = this.convertAttriputesForView(personSpecificAttributes);
				
				List<CustomAttripute> customerSpecificAttributesForView = new ArrayList<CustomAttripute>();
				if(PersonService.isPersonACustomer(personId)){
					List<Subject_attribute> customerSpecificAttributes = PersonService.getCustomerSpecificAttributesForPerson(personId);
					customerSpecificAttributesForView = this.convertAttriputesForView(customerSpecificAttributes);
				}
				
				// Start formatting contacts
				List<Contact> contacts = PersonService.getAllPersonContactsWithContact_type(personId); 
				
				
				// End formatting contacts
				request.setAttribute ("PersonId", request.getParameter("id"));
				request.setAttribute ("Command", "EditPersonCommand");
				request.setAttribute ("MainAddress", mainAddress);
				request.setAttribute ("AdditionalAddresses", additionalAddresses);
				request.setAttribute ("PersonSpecificAttributes", personSpecificAttributesForView);
				request.setAttribute ("Contacts", contacts);
				
				String isCustomerStr="N";
				if(PersonService.isPersonACustomer(personId)){
					isCustomerStr="Y";
				}
				request.setAttribute ("IsCustomer", isCustomerStr);
				request.setAttribute ("CustomerSpecificAttributes", customerSpecificAttributesForView);
				
			}
			
		}catch(Exception ex){
			System.out.println(ex.getStackTrace());
			System.out.println(ex.getMessage());
			
		}
		
		return 0;
	}

	private List<CustomAttripute> convertAttriputesForView(
			List<Subject_attribute> attributes) {
		 
		List<CustomAttripute> attributesForView = new ArrayList<CustomAttripute>();
			
		for (Subject_attribute attripute : attributes){
			CustomAttripute customAttripute = new CustomAttripute();
			customAttripute.setAttriputeId(attripute.getSubject_attribute());
			customAttripute.setAttriputeName(attripute.getSubject_attribute_type().getType_name());
			customAttripute.setAttriputeType(attripute.getData_type());
			
			if(attripute.getData_type()==SubjectDBTypeTablesConstants.ATTRIPUTE_DATA_TYPE_DATE){
				customAttripute.setAttriputeValue(attripute.getValue_date().toString());
			}else if(attripute.getData_type()==SubjectDBTypeTablesConstants.ATTRIPUTE_DATA_TYPE_NUMERIC){
				customAttripute.setAttriputeValue(String.valueOf(attripute.getValue_number()));
			}else if(attripute.getData_type()==SubjectDBTypeTablesConstants.ATTRIPUTE_DATA_TYPE_TEXT){
				customAttripute.setAttriputeValue(attripute.getValue_text());
			}
			
			if(customAttripute.getAttriputeValue()==null){
				customAttripute.setAttriputeValue("");
			}
			attributesForView.add(customAttripute);
		}
		return attributesForView;
	}

}
